package zhoufu.sz.sanxiao_check.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.widget.Toast;
import com.cy.src.update.parser.DefaultXmlInfo;
import ezy.boost.update.IUpdateParser;
import ezy.boost.update.UpdateInfo;
import ezy.boost.update.UpdateManager;
import java.io.ByteArrayInputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import zhoufu.sz.sanxiao_check.config.Constant;

/**
 * 作 者：   lwg
 * 时 间：   17-10-31 下午2:47
 *
 * @version ${VERSION}
 *          描 述：
 * @org
 */
public class CheckUpdate {
  public static void check(final Activity activity,final boolean showTips) {
    int versionCode = 0;
    try {
      versionCode =
          activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
    }
    final int version = versionCode;
    new UpdateManager.Builder(activity).setChecker(new UpdateChecker()).setParser(new IUpdateParser() {
      @Override public UpdateInfo parse(String s) throws Exception {
        DefaultXmlInfo defaultXmlInfo = parserXml(s);
        UpdateInfo updateInfo = new UpdateInfo();
        if (Integer.parseInt(defaultXmlInfo.androidversion) < version) {
          if (showTips) {
            activity.runOnUiThread(new Runnable() {
              @Override public void run() {
                Toast.makeText(activity, "已经是最新版本", Toast.LENGTH_SHORT).show();
              }
            });
          }
          return updateInfo;
        }
        updateInfo.isAutoInstall = true;
        updateInfo.isForce = true;
        updateInfo.isIgnorable = !updateInfo.isForce;
        updateInfo.updateContent = defaultXmlInfo.getAndroidcontent();
        updateInfo.versionCode = Integer.parseInt(defaultXmlInfo.androidversion);
        updateInfo.hasUpdate = version != updateInfo.versionCode;
        updateInfo.versionName = defaultXmlInfo.version;
        updateInfo.url = defaultXmlInfo.androidurl;
        updateInfo.size = 10 * 1024 * 1024;
        updateInfo.md5 = "1";
        return updateInfo;
      }
    }).setUrl(Constant.UPDATE_URL).check();
  }

  public static DefaultXmlInfo parserXml(String result) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(result.getBytes());
    boolean done = false;
    DefaultXmlInfo xmlInfo = null;
    try {
      XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
      pullParser.setInput(byteArrayInputStream, "UTF-8");
      byteArrayInputStream.close();
      String e = null;
      String nextText = null;
      for (xmlInfo = new DefaultXmlInfo(); !done; pullParser.next()) {
        int type = pullParser.getEventType();
        switch (type) {
          case XmlPullParser.START_DOCUMENT:
            e = pullParser.getName();
            break;
          case XmlPullParser.END_DOCUMENT:
            e = pullParser.getName();
            done = true;
            break;
          case XmlPullParser.START_TAG:
            e = pullParser.getName();
            if (!"update".equals(e)) {
              nextText = pullParser.nextText();

              if (e.equals("androidversion")) {
                xmlInfo.setAndroidversion(nextText);
              }

              if (e.equals("androidcontent")) {
                xmlInfo.setAndroidcontent(nextText);
              }

              if (e.equals("updatedate")) {
                xmlInfo.setUpdatedate(nextText);
              }
              if (e.equals("isUpdate")) {
                xmlInfo.setIsUpdate(nextText);
              }
              if (e.equals("size")) {
                xmlInfo.setSize(nextText);
              }
              if (e.equals("version")) {
                xmlInfo.setVersion(nextText);
              }
              if (e.equals("androidurl")) {
                xmlInfo.setAndroidurl(nextText);
              }
            }
            break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return xmlInfo;
  }
}
